package db

import (
	"go_code/gin-blog/model"
)

////查询多个分类
//func GetCategoryList(categoryIds []int64) (categoryList []*model.Category, err error) {
//	sqlstr, args, err := sqlx.In("select id,category_name,category_no from category where id in(?)", categoryIds)
//	if err != nil {
//		return nil,err
//	}
//	// 查询
//	err = DB.Select(&categoryList, sqlstr, args)
//	return categoryList,nil
//}

//1.查询多个分类
func GetCategoryList(categoryIds []int64) ([]*model.Category, error) {
	//fmt.Println(categoryIds)
	categoryList := []*model.Category{}
	if err := DB.Table("category").Where("category_id in (?)", categoryIds).Find(&categoryList).Error; err != nil {
		return nil, err
	}
	return categoryList, nil
}

////查询所有分类
//func GetAllCategoryList() (categoryList []*model.Category,err error) {
//	sqlStr := "select id,category_name,category_no from category order by category_no asc"
//	if err := DB.Select(&categoryList,sqlStr);err != nil {
//		return nil,err
//	}
//	return categoryList,nil
//}

//2.查询所有分类
func GetAllCategoryList() (categoryList []*model.Category, err error) {
	if err := DB.Table("category").Find(&categoryList).Error; err != nil {
		return nil, err
	}
	return categoryList, nil
}

////查询单个分类
//func GetCategoryById(categoryId int64) (*model.Category,error) {
//	sqlStr := `select id,category_name,category_no from category where id = ?`
//	category := &model.Category{}
//	if err := DB.Get(category,sqlStr,categoryId);err != nil{
//		return nil, err
//	}
//	return category,nil
//}

//3.查询单个分类，用于生成detail结构体
func GetCategoryById(categoryId int64) (*model.Category, error) {
	category := &model.Category{}
	if err := DB.Table("category").Where("category_id=?", categoryId).First(category).Error; err != nil {
		return nil, err
	}
	return category, nil
}
